/*******************************************************************************
 * Copyright (c) 2012 March and Conquest.
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/
package mrusanov.towers.main.ui;

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.SwingConstants;

import mrusanov.towers.cards.AbstractCard;
import mrusanov.towers.localization.Localization;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class CardButton extends JPanel {
	private static final long serialVersionUID = -1878184767080890502L;

	private static final Logger LOG = LoggerFactory.getLogger(CardButton.class);

	private final AbstractCard card;
	private final MainForm parent;

	public CardButton(final MainForm parent, final AbstractCard card) {
		super();
		this.card = card;
		this.parent = parent;

		MouseListener mouseListener = new MouseAdapter() {

			@Override
			public void mousePressed(MouseEvent e) {
				if (!parent.isCurrentPlayerMove()) {
					LOG.debug("Card {} clicked but not it is not this player turn", card);
					return;
				}

				if (e.getButton() == MouseEvent.BUTTON1) {
					if (isCardActive()) {
						LOG.debug("Playing card {} from ui", card);
						parent.currentPlayerPlaysCard(card);
					} else {
						LOG.debug("Not able to play card {} from ui because it is not active.", card);
					}
				} else {
					if (card.canBeThrownAway()) {
						LOG.debug("Throwing card {} from ui", card);
						parent.currentPlayerThrowsCard(card);
					} else {
						LOG.debug("Card {} cannot be thrown ", card);
					}
				}
			}
		};

		addMouseListener(mouseListener);

		JLabel cardName = new JLabel(Localization.getString(card.getNameMessageKey()));
		cardName.addMouseListener(mouseListener);
		cardName.setHorizontalAlignment(SwingConstants.CENTER);
		cardName.setAlignmentX(Component.CENTER_ALIGNMENT);

		JTextArea cardDescription = new JTextArea(Localization.getString(card.getDescriptionMessageKey()));
		cardDescription.setEditable(false);
		cardDescription.addMouseListener(mouseListener);
		cardDescription.setLineWrap(true);
		cardDescription.setWrapStyleWord(true);
		cardDescription.setAlignmentX(Component.CENTER_ALIGNMENT);

		BoxLayout layout = new BoxLayout(this, BoxLayout.Y_AXIS);
		setLayout(layout);

		this.add(cardName);
		this.add(Box.createVerticalGlue());
		this.add(cardDescription);
		this.add(Box.createVerticalGlue());
		this.add(Box.createVerticalGlue());
		JLabel costLabel = new JLabel(card.getCost().getLocalizedString());
		costLabel.setHorizontalAlignment(SwingConstants.CENTER);
		costLabel.addMouseListener(mouseListener);
		costLabel.setAlignmentX(Component.CENTER_ALIGNMENT);

		this.add(costLabel);

		setBorder(BorderFactory.createLineBorder(Color.BLACK, 3));

		Color cardColor = getCardColor();
		setBackground(cardColor);
		cardName.setBackground(cardColor);
		cardDescription.setBackground(cardColor);
		costLabel.setBackground(cardColor);
	}

	private boolean isCardActive() {
		return parent.isCardActive(card);
	}

	@Override
	public void paint(Graphics g) {
		if (isCardActive()) {
			super.paint(g);
		} else {
			Graphics2D g2 = (Graphics2D) g.create();
			g2.setColor(Color.BLACK);
			g2.fillRect(0, 0, getWidth(), getHeight());

			g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f));
			super.paint(g2);
			g2.dispose();
		}
	}

	@Override
	public String toString() {
		return card.getClass().getSimpleName();
	}

	private Color getCardColor() {
		if (card.getClass().getCanonicalName().contains("bricks")) {
			return new Color(0xFF8080);
		} else if (card.getClass().getCanonicalName().contains("gems")) {
			return new Color(0x8080FF);
		} else if (card.getClass().getCanonicalName().contains("beasts")) {
			return new Color(0x80FF80);
		} else {
			return Color.WHITE;
		}
	}
}
